package com.it.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.pojo.Clazz;
import com.it.mapper.ClazzMapper;
import com.it.pojo.ClazzQueryParam;

import com.it.pojo.PageResult;
import com.it.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 班级管理
 */
@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageResult page(ClazzQueryParam clazzQueryParam) {
        //1.设置分页参数(PageHelper)
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());

        //2.执行查询
        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);

        //3.解析查询结果，并封装
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageResult<Clazz>(p.getTotal(),p.getResult());
    }

    //新增班级接口实现
    @Override
    public void save(Clazz clazz){
        //补 全基础属性
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //插入班级数据(引导mapper具体实现插入)
        clazzMapper.insert(clazz);
    }

    //根据ID查询班级接口实现
    @Override
    public Clazz getInfo(Integer id){
        return clazzMapper.getInfo(id);
    }

    //更新班级接口实现
    @Override
    public void update(Clazz clazz){
        //补 全基础属性
        clazz.setUpdateTime(LocalDateTime.now());
        //更新/保存班级
        clazzMapper.update(clazz);
    }

    //删除班级接口实现
    @Override
    public void deleteById(Integer id){
        clazzMapper.deleteById(id);
    }

    //查询所有班级接口实现
    @Override
    public List<Clazz> findAll() {
        return clazzMapper.findAll();
    }
}
